package com.gcorp.idealgas;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

class LineBaffle implements Baffle
{
    
    private Line2D.Float baffle;
    
    private Vector2 normal;
    
    private Color color;
    
    private static final Vector2 YNormal = new Vector2(0.0f,1.0f);
    
    private static final Vector2 XNormal = new Vector2(1.0f,0.0f);
    
    LineBaffle(float x1,float y1,float x2,float y2,boolean isVertical)
    {
        baffle = new Line2D.Float(x1,y1,x2,y2);
        
        normal = isVertical ? YNormal : XNormal;
        
        color = new Color(0,0,0);
    }

    @Override
    public boolean isCollised(Molecule m,myPoint move) 
    {
        if(baffle.intersects(move.getX(), move.getY(), m.getRadius()*2, m.getRadius()*2))
        {
            return true;
        }
        return false;
    }
    public void setColor(Color color)
    {
        this.color = color;
    }

    @Override
    public void draw(Graphics2D g2d) 
    {
        g2d.setColor(color);
        g2d.drawLine((int)baffle.getX1(), (int)baffle.getY1(),(int) baffle.getX2(), (int)baffle.getY2());
        
    }
    public boolean checkBounce(Molecule m,Vector2 vector)
    {
        float angle = m.getVelocity().getAngleWith(normal);
        
        if(((angle >= 0 && angle <= 180) || (-angle >= 0 && -angle <= 180)) && (normal == vector))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    @Override
    public boolean isXReversed(Molecule m) 
    {
        if(checkBounce(m,XNormal))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public boolean isYReversed(Molecule m) 
    {
        if(checkBounce(m,YNormal))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
}